import React from 'react';
import {connect} from 'react-redux';
import {Form, Badge, Button, Popover, message, Input, Switch, Icon} from 'antd';
const FormItem = Form.Item;
import {checkTabDisabledV2, messages} from "share/common";
import AdvancedTable from "components/advanced-table";
import CCService from 'containers/setting/cost-center/cost-center.service';
import SlideFrame from "components/slide-frame";
import CostCenterItemRoleDetail from "containers/setting/cost-center/cost-center-item/cost-center-item-role-detail";
import LanguageInput from "components/template/language-input/language-input";

class CostCenterItemRole extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      data: [],
      columns: [
        {
          title: messages('basic-5.key839')/*成本中心项角色编码*/, dataIndex: 'positionCode', render: (text) => {
            return (
              <Popover placement="topLeft" content={text}>{text}</Popover>
            )
          }
        },
        {
          title: messages('basic-5.key840')/*成本中心项角色名称*/, dataIndex: 'positionName', render: (text) => {
            return (
              <Popover placement="topLeft" content={text}>{text}</Popover>
            )
          }
        },
        {
          title: messages('common.column.status')/*状态*/,
          dataIndex: 'enabled',
          width: 200,
          render: recode => (
            <Badge status={recode ? 'success' : 'error'}
                   text={recode ? messages('common.status.enable')/*启用*/ : messages('common.status.disable')/*禁用*/}/>
          )
        },
      ],
      record: {},
    };
  }

  componentDidMount() {
  }

  //得到列表数据
  getList = (page, size, searchParams) => {
    searchParams.page = page;
    searchParams.size = size;
    return CCService.getCostCenterItemRole(searchParams)
  };

  afterCloseSlide = () => {
    this.advancedTableRef.getList();
  };

  //新增成本中心
  handleNew = (record) => {
    if(record.id){
      CCService.getCostCenterItemRoleDetail(record.id)
        .then(res => {
          SlideFrame.open({
            title: messages('basic-5.key841')/*编辑成本中心项角色*/,
            content: CostCenterItemRoleDetail,
            afterClose: this.advancedTableRef.getList,
            params: {record: res.data},
          }, this);
        })
        .catch(err => {

        })
    }else {
      SlideFrame.open({
        title: messages('basic-5.key842')/*新建成本中心项角色*/,
        content: CostCenterItemRoleDetail,
        afterClose: this.advancedTableRef.getList,
        params: {record},
      }, this);
    }
  };

  render() {
    const {columns, loading} = this.state;
    let disabled = checkTabDisabledV2("cost-center-tab", ["authPolicyAll", "setOfBook"]);
    return (
      <div className="cost-center-item-role">
        <div className="full-height-page" style={{height: 'calc(100vh - 100px)'}}>
          <AdvancedTable
            index
            loading={loading}
            onRef={ref => this.advancedTableRef = ref}
            title={messages('basic-5.key843')/*设置成本中心项角色*/}
            searchForm={[]}
            columns={columns}
            service={this.getList}
            onRow={ record => ({ onClick: () => this.handleNew(record) })}
            rowKey="id"
            tableName='cost-center-item-role'
          >
            <div className="table-header-buttons">
              <Button type='primary'
                      onClick={()=>this.handleNew({})}
                      disabled={disabled}>
                {messages('basic-5.key842')/*新建成本中心项角色*/}
              </Button>
            </div>
          </AdvancedTable>
        </div>
        <a onClick={() => {window.history.back()}} className='inline-gap'>
          <Icon type="rollback" className='right-gap'/>
          {messages('common.back')/*返回*/}
        </a>
      </div>
    )
  }

}

CostCenterItemRole.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {
    currentMenuPermission: state.main.currentMenuPermission
  }
}

export default connect(mapStateToProps)(CostCenterItemRole);
